package com.lei.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lei.common.R;
import com.lei.dto.SetmealDto;
import com.lei.entity.Category;
import com.lei.entity.Setmeal;
import com.lei.service.CategoryService;
import com.lei.service.SetmealDishService;
import com.lei.service.SetmealService;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

/**
 * @author lei
 * @verson:1.8
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;


    /**
     * 新增套餐
     *
     * @param setmealDto
     * @return
     */
    @PostMapping
    @CacheEvict(value = "setmealCache", allEntries = true)
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        setmealService.saveWithDish(setmealDto);

        return R.success("添加成功");
    }

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> select(Integer page, Integer pageSize, String name) {
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);

        Page<SetmealDto> DtoPage = new Page<>();
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null, Setmeal::getName, name);
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(setmealPage, queryWrapper);

        //对象拷贝
        BeanUtils.copyProperties(setmealPage, DtoPage, "records");
        List<Setmeal> records = setmealPage.getRecords();

        List<SetmealDto> list = records.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item, setmealDto);
            //分类id
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if (category != null) {
                //分类名称
                String name1 = category.getName();
                setmealDto.setCategoryName(name1);
            }
            return setmealDto;

        }).collect(Collectors.toList());
        DtoPage.setRecords(list);

        return R.success(DtoPage);
    }

    /**
     * 根据条件查询对应的套餐数据
     *
     * @param
     * @return
     */
    @GetMapping("/list")
    @Cacheable(value = "setmealCache", key = "setmeal.categoryId+'_'+#setmeal.status")
    public R<List<Setmeal>> list(Setmeal setmeal) {

        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId());
        queryWrapper.eq(Setmeal::getStatus, 1);


        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);

    }


    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(value = "setmealCache", allEntries = true)
    public R<String> delete(Long[] ids) {
        ArrayList<Long> setmeals = new ArrayList<>();
        for (Long id : ids) {
            setmeals.add(id);
        }
        setmealService.removeByIds(setmeals);
        return R.success("删除成功");
    }

    /**
     * 套餐停售
     *
     * @param ids
     * @return
     */
    @PostMapping("/status/0")
    public R<String> status0(Long[] ids) {
        ArrayList<Setmeal> setmeals = new ArrayList<>();
        for (Long id : ids) {

            Setmeal byId = setmealService.getById(id);
            byId.setStatus(0);
            setmeals.add(byId);
        }
        setmealService.updateBatchById(setmeals);
        return R.success("停售成功");
    }

    /**
     * 套餐启售
     *
     * @param ids
     * @return
     */
    @PostMapping("/status/1")
    public R<String> status1(Long[] ids) {
        ArrayList<Setmeal> setmeals = new ArrayList<>();
        for (Long id : ids) {
            Setmeal byId = setmealService.getById(id);
            byId.setStatus(1);
            setmeals.add(byId);
        }
        setmealService.updateBatchById(setmeals);
        return R.success("启售成功");
    }

    /**
     * 根据id查询
     *
     * @param
     */
    @GetMapping("{id}")
    public R<Setmeal> g(@PathVariable Long id) {
        SetmealDto byIdWithDish = setmealService.getByIdWithDish(id);
        return R.success(byIdWithDish);

    }

    /**
     * 修改
     *
     * @param dto
     * @return
     */
    @PutMapping
    @CacheEvict(value = "setmealCache", allEntries = true)
    public R<String> update(@RequestBody SetmealDto dto) {
        setmealService.updateWithDish(dto);
        return R.success("修改");

    }

}
